package com.fjwt.gz.proxy.ctrl.award.virtual;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.AwardVirtualOutsideDTO;
import com.fjwt.gz.db.vo.AwardVirtualOutsideVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AwardVirtualOutsideService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 *  虚拟商品外部商品表
 */
@Slf4j
@RestController
@RequestMapping("/api/virtual/outside")
public class AwardVirtualOutsideController extends CommonCtrl {

    @Autowired
    private AwardVirtualOutsideService awardVirtualOutsideService;

    /**
     * 获取外部商品列表数据
     * @return 分页列表
     */
    @GetMapping
    public ApiRes list() {
        AwardVirtualOutsideDTO virtualOutsideDTO = getObject(AwardVirtualOutsideDTO.class);
        log.info("机构端外部商品列表，请求地址：/api/virtualGoods/outside，请求参数：{}", JSONUtil.toJsonStr(virtualOutsideDTO));
        IPage<AwardVirtualOutsideVO> page = awardVirtualOutsideService.queryList(getIPage(true), virtualOutsideDTO);
        return ApiRes.page(page);
    }

    /**
     * 刷新商品信息
     */
    @PostMapping(value = "/refreshGoods")
    public ApiRes refreshGoods() {
        AwardVirtualOutsideDTO virtualOutsideDTO = getObject(AwardVirtualOutsideDTO.class);
        log.info("机构端外部商品刷新商品信息，请求地址：/api/virtualGoods/outside/refreshGoods，请求参数：{}", JSONUtil.toJsonStr(virtualOutsideDTO));
        awardVirtualOutsideService.refreshGoodsDetail(virtualOutsideDTO);
        return ApiRes.ok();
    }

    /**
     * 商品订阅
     */
    @PostMapping(value = "/goodsSubscribe")
    public ApiRes goodsSubscribe() {
        AwardVirtualOutsideDTO virtualOutsideDTO = getObject(AwardVirtualOutsideDTO.class);
        log.info("机构端外部商品订阅，请求地址：/api/virtualGoods/outside/goodsSubscribe，请求参数：{}", JSONUtil.toJsonStr(virtualOutsideDTO));
        awardVirtualOutsideService.goodsSubscribe(virtualOutsideDTO);
        return ApiRes.ok();
    }

    /**
     * 商品取消订阅
     */
    @PostMapping(value = "/goodsUnSubscribe")
    public ApiRes goodsUnSubscribe() {
        AwardVirtualOutsideDTO virtualOutsideDTO = getObject(AwardVirtualOutsideDTO.class);
        log.info("机构端外部商品取消订阅，请求地址：/api/virtualGoods/outside/goodsUnSubscribe，请求参数：{}", JSONUtil.toJsonStr(virtualOutsideDTO));
        awardVirtualOutsideService.goodsUnSubscribe(virtualOutsideDTO);
        return ApiRes.ok();
    }


    /**
     * 外部商品添加到内部商品中
     * @return
     */
    // @PreAuthorize("hasAnyAuthority('ENT_VIRTUAL_GOODS_OUTSIDE_TO_INSIDE')")
    @PostMapping("/outToInsideGoods")
    public ApiRes outToInsideGoods() {
        AwardVirtualOutsideDTO virtualOutsideDTO = getObject(AwardVirtualOutsideDTO.class);
        log.info("机构端外部商品添加到内部商品中，请求地址：/api/virtualGoods/outside/outToInsideGoods，请求参数：{}", JSONUtil.toJsonStr(virtualOutsideDTO));
        awardVirtualOutsideService.outsideToInsideGoods(virtualOutsideDTO, getCurrentUser().getSysUserId());
        return ApiRes.ok();
    }

    /**
     * 外部商品添加到内部商品中 批量
     * @return
     */
    @PostMapping("/outToInsideGoodsBatch")
    public ApiRes outToInsideGoodsBatch() {
        String virtualOutsideIds = getValString("virtualOutsideIds");
        log.info("机构端外部商品添加到内部商品中 批量，请求地址：/api/virtualGoods/outside/outToInsideGoodsBatch，请求参数：{}", virtualOutsideIds);
        awardVirtualOutsideService.outsideToInsideGoodsBatch(virtualOutsideIds, getCurrentUser().getSysUserId());
        return ApiRes.ok();
    }

}

